package com.wanjia.seal.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.wanjia.seal.manager.annotation.LoginToken;
import com.wanjia.seal.manager.controller.ex.DataNotFoundException;
import com.wanjia.seal.manager.entity.CustomInfor;
import com.wanjia.seal.manager.entity.CustomInforVO;
import com.wanjia.seal.manager.mapper.CustomInforMapper;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/api")
@Api(tags = "客户信息表查询")
public class CustomInforController  extends BaseController{
    @Autowired
    CustomInforMapper customInforMapper;

    @ResponseBody
    @PostMapping("/getCustomInfor")
    @LoginToken
    public JsonResult<Object> findAllCustomInfor(@RequestBody CustomInfor customInfor, String startTime, String endTime,  Integer pageNum, Integer pageSize){
        String Account = customInfor.getAccount();
        String Bank = customInfor.getBank();
        String Status = customInfor.getStatus();
        String CreateOperID = customInfor.getCreateOperID();
        String UpdateOperID = customInfor.getUpdateOperID();
        String SealCardNo = customInfor.getSealCardNo();
        String Property = customInfor.getProperty();
        List<String> list = Arrays.asList(Bank.split(","));
        if (startTime==null){
            startTime ="1988-01-01";
        }
        if (endTime==null){
            endTime="2099-01-01";
        }
        if (pageNum != null){
            //List<CustomInforVO> result = customInforMapper.findByCustomInfor(Account, Status, list, Property,CreateOperID, UpdateOperID, startTime, endTime, SealCardNo,pageNum,pageSize);
            List<CustomInforVO> result2 = customInforMapper.findByCustomInfors(Account, Status, list, Property,CreateOperID, UpdateOperID, startTime, endTime, SealCardNo);
            List<CustomInforVO>  userList2 = result2.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(
                                    () -> new TreeSet<>(
                                            Comparator.comparing(CustomInforVO::getCustomName)
                                    )
                            ), treeSet -> new ArrayList<>(treeSet)
                    )
            );
            List<CustomInforVO>  userList3 = userList2.stream().sorted(Comparator.comparing(CustomInforVO::getCreateDate).reversed()).collect(Collectors.toList());
            List<CustomInforVO> subList = userList3.stream().skip((pageNum-1)*pageSize).limit(pageSize).collect(Collectors.toList());
            int size = userList2.size();
           // int size = result2.size();
            /*List<CustomInforVO>  userList = result.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(
                                    () -> new TreeSet<>(
                                            Comparator.comparing(CustomInforVO::getCustomName)
                                    )
                            ), treeSet -> new ArrayList<>(treeSet)
                    )
            );*/
            //int size = userList.size();
            //新增账户名称重复，按照创建时间选取最新一条
            Map<String, CustomInforVO> map = new HashMap<>();
            for (CustomInforVO custom : subList) {
                String key = custom.getCustomName();
                if (map.containsKey(key)) {
                    CustomInforVO oldCustom = map.get(key);
                    if (custom.getCreateDate().getTime() > oldCustom.getCreateDate().getTime()) {
                        map.put(key, custom);
                    }
                } else {
                    map.put(key, custom);
                }
            }

            JSONObject jsonObject=new JSONObject();
            jsonObject.put("total",size);
            jsonObject.put("CustomInfor",new ArrayList<>(map.values()));
            return new JsonResult<>(OK,jsonObject);
        }else{
            List<CustomInforVO> result = customInforMapper.findByCustomInfors(Account, Status, list, Property,CreateOperID, UpdateOperID, startTime, endTime, SealCardNo);
            List<CustomInforVO>  userList = result.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(
                                    () -> new TreeSet<>(
                                            Comparator.comparing(CustomInforVO::getCustomName)
                                    )
                            ), treeSet -> new ArrayList<>(treeSet)
                    )
            );
            List<CustomInforVO>  userList3 = userList.stream().sorted(Comparator.comparing(CustomInforVO::getCreateDate).reversed()).collect(Collectors.toList());
            int size = userList.size();
            //新增账户名称重复，按照创建时间选取最新一条
            Map<String, CustomInforVO> map = new HashMap<>();
            for (CustomInforVO custom : userList3) {
                String key = custom.getCustomName();
                if (map.containsKey(key)) {
                    CustomInforVO oldCustom = map.get(key);
                    if (custom.getCreateDate().getTime() > oldCustom.getCreateDate().getTime()) {
                        map.put(key, custom);
                    }
                } else {
                    map.put(key, custom);
                }
            }

            JSONObject jsonObject=new JSONObject();
            jsonObject.put("total",size);
            jsonObject.put("CustomInfor",new ArrayList<>(map.values()));
            return new JsonResult<>(OK,jsonObject);
        }
        /*List<CustomInforVO> result = customInforMapper.findByCustomInfors(Account, Status, list, Property,CreateOperID, UpdateOperID, startTime, endTime, SealCardNo);
        List<CustomInforVO>  userList = result.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(
                                () -> new TreeSet<>(
                                        Comparator.comparing(CustomInforVO::getCustomName)
                                )
                        ), treeSet -> new ArrayList<>(treeSet)
                )
        );
        int size = userList.size();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",size);
        jsonObject.put("CustomInfor",userList);
        return new JsonResult<>(OK,jsonObject);*/

    }
}
